package com.moinut.studentattendancemanager.view.teacher;

import com.moinut.studentattendancemanager.config.UserType;
import com.moinut.studentattendancemanager.controller.LoginController;
import com.moinut.studentattendancemanager.view.BaseFrame;
import com.moinut.studentattendancemanager.utils.WindowJumper;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/**
 * @author MOILING
 */
public class TeacherLoginFrame extends BaseFrame<TeacherLoginFrame.TeacherLoginPanel> {
    private static final long serialVersionUID = 1L;

    private static final int DEFAULT_WIDTH = 300;
    private static final int DEFAULT_HEIGHT = 320;

    public TeacherLoginFrame() {
        super("教师登录", DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }

    @Override
    protected TeacherLoginPanel onSetContentPane() {
        return new TeacherLoginPanel();
    }

    /*package*/ class TeacherLoginPanel extends JPanel implements ActionListener {
        private static final long serialVersionUID = 1L;

        private JLabel mPicLabel;
        private JLabel mIdLabel, mPasswordLabel;
        private JTextField mTeacherIdTextField;
        private JPasswordField mPasswordField;
        private JButton mOkButton, mClearButton;
        private JPanel mButtonPanel, mInfoPanel;
        private JCheckBox mAdminCheckBox;

        /*package*/ TeacherLoginPanel() {
            mPicLabel = new JLabel();
            mIdLabel = new JLabel("工号：");
            mPasswordLabel = new JLabel("密码：");
            mTeacherIdTextField = new JTextField(20);
            mPasswordField = new JPasswordField(20);
            mOkButton = new JButton("登录");
            mClearButton = new JButton("清空信息");

            mButtonPanel = new JPanel();
            mInfoPanel = new JPanel();

            mAdminCheckBox = new JCheckBox("我是管理员");

            setLayout(new BorderLayout());

            mInfoPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
            mPicLabel.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT / 2);

            ImageIcon image = new ImageIcon(this.getClass().getResource("/res/images/login_head.jpg"));
            image.setImage(image.getImage().getScaledInstance(mPicLabel.getWidth(),
                    mPicLabel.getHeight(),
                    Image.SCALE_DEFAULT));
            mPicLabel.setIcon(image);

            mInfoPanel.add(mPicLabel);
            mInfoPanel.add(mIdLabel);
            mInfoPanel.add(mTeacherIdTextField);
            mInfoPanel.add(mPasswordLabel);
            mInfoPanel.add(mPasswordField);
            mInfoPanel.add(mAdminCheckBox);

            add(mInfoPanel, BorderLayout.CENTER);

            mButtonPanel.add(mOkButton);
            mButtonPanel.add(mClearButton);
            add(mButtonPanel, BorderLayout.SOUTH);

            mOkButton.addActionListener(this);
            mClearButton.addActionListener(this);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(mOkButton)) {
                LoginController.login(mTeacherIdTextField.getText(), new String(mPasswordField.getPassword()), UserType.TYPE_TEACHER, new LoginController.OnLoginListener() {
                    @Override
                    public void onLoginSuccess() {
                        WindowJumper.jump(TeacherLoginFrame.this, TeacherPageFrame.class);
                    }

                    @Override
                    public void onLoginError(String error) {
                        JOptionPane.showMessageDialog(null, error, "错误", JOptionPane.ERROR_MESSAGE);
                        mPasswordField.setText("");
                    }
                });
            } else if (e.getSource().equals(mClearButton)) {
                mTeacherIdTextField.setText("");
                mPasswordField.setText("");
            }
        }
    }
}
